package com.ruoxi.blog.test;

import java.util.Arrays;

/**
	* @author YaoXian
	* @title: MergeArray
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 合并两个有序数组,并找出中位数
	*   - 对应力扣难度 - 困难
	* @date 2021-03-25 16:27
	*/
public class MergeArray {
		public static void main(String[] args) {
				int[] a = {1};
				int[] b = {2};
				System.out.println(merge(a,b));
		}
		
		public static double merge(int[] nums1,int[] nums2){
				int aTemp = 0;
				int bTemp = 0;
				int i = 0;
				int [] arr = new int[nums1.length+nums2.length];
				while (aTemp < nums1.length  && bTemp < nums2.length){
						if (nums1[aTemp] <= nums2[bTemp]){
								arr[i] = nums1[aTemp];
								aTemp++;
						}else {
								arr[i] = nums2[bTemp];
								bTemp++;
						}
						i++;
				}
				while (aTemp < nums1.length){
						arr[i] = nums1[aTemp];
						aTemp++;
						i++;
				}
				while (bTemp < nums2.length){
						arr[i] = nums2[bTemp];
						bTemp++;
						i++;
				}
				
				double result;
				
				if (arr.length % 2 == 0){
						result =((double)arr[arr.length/2] + arr[arr.length/2-1])/2;
				}else {
						result = (double)arr[(arr.length-1)/2];
				}
				System.out.println(Arrays.toString(arr));
				return result;
		}
		
}
